#pragma once
#include <curand_kernel.h>
#include <cuda.h>

__device__ static void dev_swap(float &a, float &b)
{
    float t = a;
    a = b;
    b = t;
}

__device__ static float dev_max(float a, float b)
{
    return a > b ? a : b;
}

__device__ static float dev_min(float a, float b)
{
    return a < b ? a : b;
}

__device__ static int random_count = 0;
__device__ static float get_random_float(int some_id)
{
    curandState devStates;
    curand_init(some_id + random_count, some_id + random_count++, 0, &devStates);
    float rd = curand_uniform(&devStates);
    return rd;
}

__device__ static float dev_clamp(const float &lo, const float &hi, const float &v)
{

    return dev_max(lo, dev_min(hi, v));
}

__device__ static bool solveQuadratic(const float &a, const float &b, const float &c, float &x0, float &x1)
{
    float discr = b * b - 4 * a * c;
    if (discr < 0)
        return false;
    else if (discr == 0)
        x0 = x1 = -0.5 * b / a;
    else
    {
        float q = (b > 0) ? -0.5 * (b + sqrt(discr)) : -0.5 * (b - sqrt(discr));
        x0 = q / a;
        x1 = c / q;
    }

    if (x0 > x1)
    {
        float temp = x0;
        x0 = x1;
        x1 = temp;
    }
    // std::swap(x0, x1);
    return true;
}
